package netWork;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;

public class UDPSenderB {

    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket();
        String request = "hello,明天吃火锅";
        //请求
        DatagramPacket requestPacket = new DatagramPacket(request.getBytes(),0,
                request.getBytes().length, InetAddress.getByName("127.0.0.1"),9999);
        //发送请求
        socket.send(requestPacket);

        DatagramPacket responsePacket = new DatagramPacket(new byte[4096],0,4096);
        socket.receive(responsePacket);
        String response = new String(responsePacket.getData(),0,responsePacket.getLength());
        System.out.printf("[%s %d],req: %s  resp:%s\n",responsePacket.getAddress().getHostAddress(),responsePacket.getPort(),request,response);
        socket.close();
    }
}
